package com.gome.config;

import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Method;

@Aspect
@Configuration
public class TransactionAdviceConfig {
    private final Logger log=Logger.getLogger(this.getClass());
            //定义切面
    @Pointcut("@annotation(org.springframework.transaction.annotation.Transactional)")
    public void annotationPointCut() {}

    //声明一个advice,并使用@pointCut定义的切点
    @After("annotationPointCut()")
    public void after(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        log.info("后置拦截:" +signature.getDeclaringType()+"中"+ method.getName());

    }
    //定义方法拦截的规则
    @Before("@annotation(org.springframework.transaction.annotation.Transactional)")
    public void before(JoinPoint joinPoint) {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();

        Method method = signature.getMethod();
        log.info("前置拦截:" +signature.getDeclaringType()+"中"+ method.getName());
    }


}
